﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Ashlen.Portal.Core;
using Ashlen.Portal.Core.DTO;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Data.SqlServer;
using System.Web.Mvc;
using System.Web.Caching;
using System.Web;


namespace Ashlen.Portal.Services
{
    /// <summary>
    /// The FinanceService service contains finance related functionality. It contains News business logic and instantiates the 
    /// IFinanceDao object to interact with the database.
    /// </summary>
    /// <author>Shailen Sukul</author>
    /// <date>08 Nov 2006</date>
    /// <remarks>- JSON cannot understand LINQ-ENTITY objects (recursion problem while serializing) so had to have the methods returning anonymous type.
    /// This returns a much simpler object for ex: Select(f => new { f.Id, f.Caption ...} in the Dao layer.
    /// The return is a string type, which is actually JSON formatted as string. Look at the Ashlen.Portal.Core.Service.ServiceResponse object to learn about
    /// the response type.
    /// The decision to use JSON over XML was purely for performance reasons (JSON has a much smaller footprint).
    /// </remarks>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class LookupService : ILookupService
    {
        private ILookupDao _lookupDao;
        public LookupService()
        {
            _lookupDao = new LookupDao();
        }

        public string GetCountries() {
            ServiceResponse response = new ServiceResponse();
            try {

                OutgoingWebResponseContext Response = WebOperationContext.Current.OutgoingResponse;
                HttpResponseHeader cacheHeader = HttpResponseHeader.CacheControl;
                String cacheControlValue = String.Format("max-age={0}, must-revalidate", 1);
                Response.Headers.Add(cacheHeader, cacheControlValue);
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
                response.IsSuccess = true;
                var countries = _lookupDao.GetCountries();
                response.Payload = countries;
                response.IsSuccess = true;
            }
            catch (Exception ex) {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string GetStates(string countryId)
        {
            ServiceResponse response = new ServiceResponse();
            try
            {

                OutgoingWebResponseContext Response = WebOperationContext.Current.OutgoingResponse;
                HttpResponseHeader cacheHeader = HttpResponseHeader.CacheControl;
                String cacheControlValue = String.Format("max-age={0}, must-revalidate", 1);
                Response.Headers.Add(cacheHeader, cacheControlValue);
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
                response.IsSuccess = true;
                var states = _lookupDao.GetStates(countryId);
                response.Payload = states;
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        public string GetUsers()
        {
            ServiceResponse response = new ServiceResponse();
            try
            {

                OutgoingWebResponseContext Response = WebOperationContext.Current.OutgoingResponse;
                HttpResponseHeader cacheHeader = HttpResponseHeader.CacheControl;
                String cacheControlValue = String.Format("max-age={0}, must-revalidate", 1);
                Response.Headers.Add(cacheHeader, cacheControlValue);
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
                response.IsSuccess = true;
                var users = _lookupDao.GetUsers();
                response.Payload = users;
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response = Utility.GetErrorResponse(ex);
            }
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }
    }
}
